package game.module.hall.processor;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import game.HallServer;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageHall.C2SCheckAgent;
import lua.LuaMessageHall.S2CCheckAgent;
import sdk.common.HttpClientUtils;

@MsgCodeAnn(msgcode = C2SCheckAgent.id, accessLimit = 500)
public class AgentCheckProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(AgentCheckProcessor.class);
	private static String agentServerHost = HallServer.getInstance().getServerConfig().getAgentServerUrl();
	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		C2SCheckAgent reqMsg = C2SCheckAgent.parse(request);
		String agentId = reqMsg.agent_id;
		int playerId = hero.getId();
		logger.info("agent check,playeyId={} , agentId = {}", playerId,agentId);
		//先检查是否绑定
		HallServer.executorService.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> requestObj = new HashMap<>();
				requestObj.put("module", "bind_agent");
				requestObj.put("param", "is_agent_bind");
				requestObj.put("playerId", playerId);
				ObjectMapper objectMapper = new ObjectMapper();
				String requestParams;
				try {
					String agentServerUrl = agentServerHost;
					logger.info("query start");
					requestParams = objectMapper.writeValueAsString(requestObj);
					Map<String, Object> respObj = HttpClientUtils.getInstance().doHttpPost(agentServerUrl, requestParams);
					boolean ret = (Boolean) respObj.get("ret");
					logger.info("is_agent_bind ret={}", ret);
					if (ret==true)//已经绑定过，不能重复绑定 
					{
						//返回客户端
						S2CCheckAgent respMsg = new S2CCheckAgent("", "", 1);//1表示已经绑定
						hero.writeAndFlush(respMsg.build(hero.alloc()));
						return;
					}else
					{
						requestObj.clear();
						requestObj.put("module", "bind_agent");
						requestObj.put("param", "check_agent");
						requestObj.put("agentId", agentId);
						
						requestParams = objectMapper.writeValueAsString(requestObj);
						Map<String, Object> respObj2 = HttpClientUtils.getInstance().doHttpPost(agentServerUrl, requestParams);
						boolean ret2 = (Boolean) respObj2.get("ret");
						logger.info("check_agent ret2={}", ret2);
						if (ret2==false) //无代理商记录
						{
							//返回客户端
							S2CCheckAgent respMsg = new S2CCheckAgent("", "", -1);//-1表示失败
							hero.writeAndFlush(respMsg.build(hero.alloc()));
							return;
						}else 
						{
							String agentName = (String) respObj2.get("agentName");
							//返回客户端
							S2CCheckAgent respMsg = new S2CCheckAgent(agentId, agentName, 0);//0表示成功
							hero.writeAndFlush(respMsg.build(hero.alloc()));
							return;
						}
						
					}
					
				} catch (JsonProcessingException e) {
					logger.error("", e);
				}
			}
		});
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

	public static void main(String[] args) {
		Map<String, Object> requestObj = new HashMap<>();
		requestObj.put("module", "bind_agent");
		requestObj.put("param", "do_bind");
		requestObj.put("playerId", 169);
		requestObj.put("agentId", "agent");
		ObjectMapper objectMapper = new ObjectMapper();
		String requestParams;
		try {
			String agentServerUrl = "http://192.168.0.145:9090/GuanQuWeb/api/agent";
			logger.info("query start");
			requestParams = objectMapper.writeValueAsString(requestObj);
			long s1 = System.currentTimeMillis();
			Map<String, Object> respObj = HttpClientUtils.getInstance().doHttpPost(agentServerUrl, requestParams);
			logger.info("cost={}", System.currentTimeMillis() - s1);
			boolean ret = (Boolean) respObj.get("ret");
			logger.info("ret={}", ret);
		} catch (JsonProcessingException e) {
			logger.error("", e);
		}
		System.out.println(System.currentTimeMillis());
	}

}
